﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Core;

namespace Portal.AssetTn.Model
{
    public class AssetNationalCls
    {
        public string AssetNationalId = "";
        public string AssetNationalName = "";
    }

    public class AssetNationalParser
    {
        public static AssetNationalCls CreateInstance()
        {
            //tao 1 class trang....
            AssetNationalCls oAssetNational = new AssetNationalCls();
            return oAssetNational;
        }

        public static AssetNationalCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            AssetNationalCls oAssetNational = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oAssetNational;
        }


        public static AssetNationalCls ParseFromDataRow(DataRow dr)
        {
            AssetNationalCls oAssetNational = new AssetNationalCls();
            try
            {
                if (dr["AssetNationalId"] != DBNull.Value)
                {
                    oAssetNational.AssetNationalId = (string)dr["AssetNationalId"];
                }
            }
            catch { }
            try
            {
                if (dr["AssetNationalName"] != DBNull.Value)
                {
                    oAssetNational.AssetNationalName = (string)dr["AssetNationalName"];
                }
            }
            catch { }
            

            return oAssetNational;
        }

        public static AssetNationalCls[] ParseFromDataTable(DataTable dtTable)
        {
            AssetNationalCls[] AssetNationals = new AssetNationalCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                AssetNationals[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return AssetNationals;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet("Asset");
            ds.Tables.Add("Info");
            ds.Tables["Info"].Columns.Add("AssetNationalId");
            ds.Tables["Info"].Columns.Add("AssetNationalName");
            return ds;
        }

        public static string GetXml(AssetNationalCls oAssetNational)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                        oAssetNational.AssetNationalId,
                        oAssetNational.AssetNationalName,
                    });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            AssetNationalCls oAssetNational = ParseFromDataRow(dr);
            string Xml = GetXml(oAssetNational);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            AssetNationalCls[] AssetNationals = new AssetNationalCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                AssetNationals[iIndex] = ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = GetXml(AssetNationals);
            return Xml;
        }


        public static string GetXml(AssetNationalCls[] AssetNationals)
        {
            if (AssetNationals == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                AssetNationals = new AssetNationalCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < AssetNationals.Length; iIndex++)
            {
                AssetNationalCls oAssetNational = AssetNationals[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                     {
                         oAssetNational.AssetNationalId,
                         oAssetNational.AssetNationalName,
                    });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static AssetNationalCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new AssetNationalCls[0];
            }
            AssetNationalCls[] AssetNationals = new AssetNationalCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                AssetNationals[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return AssetNationals;
        }
    }
}
